import role from "@/api/role";
import dateChoose from "@/utils/datachoose";


export default {
    name: "index",
    created() {
        this.searchPage();
        this.getAllTreeMenu();
    },
    data() {
        return {
            searchForm: {
                currentPage: 1,
                pageSize: 5
            },

            rules: {
                roleName: [
                    {required: true, message: '角色名称不能为空', trigger: 'blur'},
                ],
            },
            pickerOptions: dateChoose.pickerOptions,
            chooseDate: dateChoose.chooseDate,
            tableData: [],
            total: 0,
            selectIds: [],
            createDialog: false,
            formData: {},
            defaultProps: {
                children: 'children',
                label: 'menuTitle'
            },
            menuList: [],
            roleId: ''
        }
    },
    methods: {
        //增删改查操作
        async searchPage() {
            let response = await role.searchPage(this.searchForm)
            this.total = response.total;
            this.tableData = response.data;
            this.$refs.huigeTree.setCheckedKeys([]);
        },

        /**
         * 添加或者修改
         */
        addOrEdit() {
            //表单手动校验
            this.$refs.form.validate(async (valid) => {
                if (valid) {
                    this.createDialog = false;
                    if (this.formData.id) {
                        //修改
                        await role.update(this.formData);
                    } else {
                        //添加
                        await role.add(this.formData);
                    }
                    this.searchPage();
                } else {
                    this.$message.error("表单校验失败")
                    return false;
                }
            });


        },

        /*
        * findById
        * */
        async findById(id) {

            this.createDialog = true;
            this.formData = await role.findById(id);


        },

        /*
        * 批量删除
        * */

        async batchDeleteByIds() {
            await role.batchDeleteByIds(this.selectIds);
            this.searchPage();
        },

        /*
        * 通过id删除
        *
        * */
        async deleteById(id) {
            await role.deleteById(id);
            this.searchPage();
        },

        /**
         * 获取所有的权限
         */
        async getAllTreeMenu() {
            this.menuList = await role.getAllMenuTreeData();
        },

        /**
         * 设置角色的权限
         */

        async setRoleMenu() {
            //给某个角色设置权限的角色id 已经拿到了
            // 必须拿到这个角色勾选的权限idssetCheckedKeys
            let result = this.$refs.huigeTree.getCheckedKeys()
            let result1 = this.$refs.huigeTree.getHalfCheckedKeys();
            let ids = result.concat(result1);
            await role.setRoleMenu(this.roleId, ids);

            this.searchPage();


        },

        //获取角色对应的权限
        async getMenusByRoleId() {
            let ids = await role.getMenusByRoleId(this.roleId);
            this.$refs.huigeTree.setCheckedKeys(ids);
        },


        ////////////////////////页面的效果/////////////////////////////////////////
        /**
         * 点击分页回调函数
         */
        pageChange(page) {
            this.searchForm.currentPage = page;
            this.searchPage();
        },
        /**
         * 选择查询时间事件
         */
        chooseDateChange(val) {
            this.searchForm.startTime = val[0];
            this.searchForm.endTime = val[1];
        },
        /**
         * 搜索重置
         */
        resetSearchForm() {
            this.searchForm = {
                currentPage: 1,
                pageSize: 5
            };
            this.chooseDate = ""
        },
        /**
         * 勾选框事件
         */
        selectChange(val) {
            this.selectIds = val.map(item => item.id);
        },
        /**
         * 设置添加按钮的点击事件
         */
        addBtnClick() {
            this.createDialog = true;
            this.formData = {};
            this.$refs.form.resetFields();
        },
        /*
        * 点击批量删除
        * */
        showBatchDeleteDialog() {
            this.$confirm('你确定要删除id为' + this.selectIds + "几个吗", '温馨提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(async () => {
                //点击确定
                await this.batchDeleteByIds(this.selectIds)
            })
        },

        /**
         * 给行设置点击事件
         */
        rowClick(row) {
            this.roleId = row.id;
            this.getMenusByRoleId();
        }

    }
}
